package nhan.busmap;

import java.io.File;
import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

public class DBAdapter {
	public static final String DATABASE_NAME = "busMapDatabase";
	
	//table STATION INFO
	public static final String TABLE_STATIONINFO = "STATIONINFO";
	public static final String COLUMN_STA_MSB = "MSB";
	public static final String COLUMN_STA_Address = "Address";
	public static final String COLUMN_STA_GPRSX = "GPRSX";
	public static final String COLUMN_STA_GPRSY = "GPRSY";
	
	//table ALGORITHMTEMP
	public static final String TABLE_ALGORITHMTEMP = "ALGORITHMTEMP";
	public static final String COLUMN_ALGTEMP_MST = "MST";
	public static final String COLUMN_ALGTEMP_LINEPROCESSING = "LINEPROCESSING";
	public static final String COLUMN_ALGTEMP_HIERARCHYLINE = "HIERARCHYLINE";
	public static final String COLUMN_ALGTEMP_FATHER = "FATHER";
	public static final String COLUMN_ALGTEMP_STT = "STT";
	
	//table ALGORITHMLINE
	public static final String TABLE_ALGORITHMLINE = "ALGORITHMLINE";
	public static final String COLUMN_ALGORITHMLINE_MST = "MST";
	public static final String COLUMN_ALGORITHMLINE_STT = "STT";
	public static final String COLUMN_ALGORITHMLINE_LINECOVER = "LINECOVER";
	
	//table BUSLINE
	public static final String TABLE_BUSLINE = "BUSLINE";
	public static final String COLUMN_BUSLINE_MST = "MST";
	public static final String COLUMN_BUSLINE_STT = "STT";
	public static final String COLUMN_BUSLINE_MSB = "MSB";
	public static final String COLUMN_BUSLINE_MSTCOVER = "MSTCOVER";
	
	//table LINEDETAIL
		public static final String TABLE_LINEDETAIL = "LINEDETAIL";
		public static final String COLUMN_LINEDETAIL_MST = "MST";
		public static final String COLUMN_LINEDETAIL_INFO = "INFO";
		
	
	SQLiteDatabase database;
	private SQLiteDatabase.CursorFactory factory;
	String pathDb;
	//ArrayList<ArrayList<String>>  run = new ArrayList<ArrayList<String>>(360) ;
	
	public void openDatabase(String packaName) {
		pathDb = Environment.getExternalStorageDirectory().toString() + "/" + DATABASE_NAME;
		
		//pathDb = "/data/data/"+ packaName + "databases/" + DATABASE_NAME;
		File file = new File(pathDb);
		if (file.exists()) {
			Log.d("DB","file ton tai"); 
		}
		else {
			Log.d("DB","file khong ton tai");
		}		
	}	
	
	public void closeDatabase() {
		database.close(); 
	}
	
	public SQLiteDatabase getReadableDatabase() {
		database= SQLiteDatabase.openDatabase(pathDb,null,SQLiteDatabase.OPEN_READONLY);
		return database;
	}
	
	public SQLiteDatabase getWritableDatabase(){
		Log.d("NHAN","getWrite");
		database= SQLiteDatabase.openDatabase(pathDb,null,SQLiteDatabase.OPEN_READWRITE);
		return database;
	}
	
	/*public ArrayList<String>getUser() {  
		getReadableDatabase(); 
		ArrayList <String> listUser= new ArrayList<String> (); 
		Cursor cursor=database.query(TABLE_USER, new String[] {COLUMN_ID, COLUMN_NAME},null,null,null,null,null); 
		if (cursor.moveToFirst()) { 
			do { 
				User temp= new User(); 
				temp.setId(Integer.parseInt(cursor.getString(0)));  
				temp.setName(cursor.getString(1));  
				listUser.add(temp); 
			} while (cursor.moveToNext());
		}
		if (cursor!=null&& !cursor.isClosed()) { 
			cursor.close();
		}
		return listUser;
	}*/
	
	
	
	//table BUSLINE
		/*public static final String TABLE_BUSLINE = "BUSLINE";
		public static final String COLUMN_BUSLINE_MST = "MST";
		public static final String COLUMN_BUSLINE_STT = "STT";
		public static final String COLUMN_BUSLINE_MSB = "MSB";
		public static final String COLUMN_BUSLINE_MSTCOVER = "MSTCOVER";*/
	//Demo truy xuat du lieu
	void getData() {
		getReadableDatabase(); 		
		Cursor cursor=database.query(TABLE_BUSLINE, new String[] {COLUMN_BUSLINE_MST}, null, null,null,null,null); 
		if (cursor.moveToFirst()) { 
			do { 				
				Log.d("NHAN",cursor.getString(0));				
			} while (cursor.moveToNext());
		}
		if (cursor!=null&& !cursor.isClosed()) { 
			cursor.close();
		}	
		closeDatabase();
	}
	
	//+++++++++ UPDATE:  21h35 9/7/2012 +++++++
	//++++++++++ TYPE: INSERT +++++++
	/*
	 * return cursor cua cac tuyen co di qua chung mot ben
	 * nho open database va close 
	 * 0 COLUMN_BUSLINE_MST
	 * 1 COLUMN_BUSLINE_MSB
	 */
	Cursor getLineSameStation(String msb) {
		//getReadableDatabase(); 		
		Cursor cursor=database.query(TABLE_BUSLINE, new String[] {COLUMN_BUSLINE_MST, COLUMN_BUSLINE_MSB},COLUMN_BUSLINE_MSB + "=" + msb, null,null,null,null); 
		Log.d("NHAN", ""+cursor.getCount());	
		return cursor;
		//closeDatabase();
	}
	//+++++++++++++++++ END +++++++++++++++++++++
	
	
	int getMSTFromString(String mst) {
		int a = Integer.getInteger(mst.substring(2));
		if (mst.charAt(0)=='A') {
			return a;
		}
		else return a*2;		
	}
	
	/*public static final String COLUMN_ALGTEMP_MST = "MST";
	public static final String COLUMN_ALGTEMP_LINEPROCESSING = "LINEPROCESSING";
	public static final String COLUMN_ALGTEMP_HIERARCHYLINE = "HIERARCHYLINE";
	public static final String COLUMN_ALGTEMP_FATHER = "FATHER";
	public static final String COLUMN_ALGTEMP_STT = "STT";*/
	// ko can open voi close database
	private void insertTaALGORITHMTEMP(String mst, String lineProcessing, int hierachyLine, String father, int stt) {
		ContentValues cv= new ContentValues();  
		cv.put(COLUMN_ALGTEMP_MST,mst);  
		cv.put(COLUMN_ALGTEMP_LINEPROCESSING,lineProcessing);  
		cv.put(COLUMN_ALGTEMP_HIERARCHYLINE, hierachyLine);
		cv.put(COLUMN_ALGTEMP_FATHER, father);
		cv.put(COLUMN_ALGTEMP_STT, stt);
		database.insert(TABLE_ALGORITHMTEMP,null,cv);
	}
	
	
	private void updateTaALGORITHMTEMP(int hierachyLine, String father, int stt) {
		ContentValues cv= new ContentValues();  		  
		cv.put(COLUMN_ALGTEMP_HIERARCHYLINE, hierachyLine);
		cv.put(COLUMN_ALGTEMP_FATHER, father);	
		database.update(TABLE_ALGORITHMTEMP, cv, COLUMN_ALGTEMP_STT +" = " + stt , null);
	}
	
	
	
	void proAlgorithmLineTable() {
		getReadableDatabase(); 
		//ArrayList <User> listUser= new ArrayList<User> (); 
		Cursor cursor=database.query(TABLE_LINEDETAIL, new String[] {COLUMN_LINEDETAIL_MST},null,null,null,null,null); 
		if (cursor.moveToFirst()) { 
			do { 				
				Log.d("NHAN", "test");				
				
			} while (cursor.moveToNext());
		}
		if (cursor!=null&& !cursor.isClosed()) { 
			cursor.close();
		}	
		
		closeDatabase();
	}
	
	//+++++++++ UPDATE:  21h35 9/7/2012 +++++++
	//++++++++++ TYPE: INSERT +++++++
	// chi de test demo thoi
	public void testDemo() {
		getReadableDatabase(); 
		getLineSameStation("8693");
		closeDatabase();		
	}
	//+++++++++++++++++ END +++++++++++++++++++++
	
	

}
